package com.wl.upload;

import com.wl.upload.bean.UploadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@AutoConfiguration
@ComponentScan("com.wl.upload")
@ConditionalOnProperty(prefix = "upload",name="enable",matchIfMissing = true)
@EnableConfigurationProperties(UploadProperties.class)
public class UploadConfiguration implements WebMvcConfigurer {

    @Autowired
    UploadProperties properties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        properties.getConfig().values().forEach(config->{
            registry.addResourceHandler(config.getWebpath())
                    .addResourceLocations("file:" + config.getDiskpath());
        });
    }
}
